﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;

namespace CSP.Web.UserControls
{
    public partial class CSPPager : CSPUserControlBase
    {

        #region 属性

        private bool _autoPostBack = false;

        private bool _isShowAppointRedirect = true;

        /// <summary>
        /// 是否自动回发服务器
        /// </summary>
        public bool AutoPostBack
        {
            get
            {
                return _autoPostBack;
            }
            set
            {
                _autoPostBack = value;
            }
        }

        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            get
            {
                return Convert.ToInt32(Txt_CurrentIndex.Value) - 1;
            }
            set
            {
                Txt_CurrentIndex.Value = (value + 1).ToString();
            }
        }

        /// <summary>
        /// 每页显示的列表数
        /// </summary>
        public int PageSize
        {
            get
            {
                return Convert.ToInt32(Txt_Items_Per_Page.Value);
            }
            set
            {
                Txt_Items_Per_Page.Value = value.ToString();
            }

        }

        /// <summary>
        /// 分页链接显示数
        /// </summary>
        public int Num_Display_Entries
        {
            get
            {
                return Convert.ToInt32(Txt_Num_Display_Entries.Value);
            }
            set
            {
                Txt_Num_Display_Entries.Value = value.ToString();
            }

        }

        /// <summary>
        /// 起始与结束点的数目
        /// </summary>
        public int Num_Edge_Entries
        {
            get
            {
                return Convert.ToInt32(Txt_Num_Edge_Entries.Value);
            }
            set
            {
                Txt_Num_Edge_Entries.Value = value.ToString();
            }

        }

        /// <summary>
        /// “上一页”标签：
        /// </summary>
        public string Prev_Text
        {
            get
            {
                return Txt_Prev_Text.Value
                    ;
            }
            set
            {
                Txt_Prev_Text.Value = value;
            }
        }

        /// <summary>
        /// “下一页”标签：
        /// </summary>
        public string Next_Text
        {
            get
            {
                return Txt_Next_Text.Value;
            }
            set
            {
                Txt_Next_Text.Value = value;
            }
        }

        /// <summary>
        /// 总数量数
        /// </summary>
        public int ItemCount
        {
            get
            {
                return Convert.ToInt32(Txt_ItemCount.Value);
            }
            set
            {
                Txt_ItemCount.Value = value.ToString();
            }
        }

        /// <summary>
        /// 是否显示指定分页
        /// </summary>
        public bool IsShowAppointRedirect
        {
            get
            {
                return _isShowAppointRedirect;
            }
            set
            {
                _isShowAppointRedirect = value;
            }
        }
        #endregion

        #region 事件

        public event EventHandler IndexChange;

        //public EventHandler IndexChange
        //{
        //    get { return _indexChange; }
        //    set { _indexChange = value; }
        //}

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AppointRedirect.Visible = _isShowAppointRedirect;
                InitPageCount();
            }
        }

        /// <summary>
        /// 初始化数据页PageCount
        /// </summary>
        private void InitPageCount()
        {
            int pageCount = 0;
            int pageSize = Convert.ToInt32(this.Txt_Items_Per_Page.Value);
            int itemCount = Convert.ToInt32(this.Txt_ItemCount.Value);
            pageCount = itemCount / pageSize;
            if (itemCount % pageSize != 0)
            {
                pageCount++;
            }
            this.Txt_Length.Value = pageCount.ToString();

        }

        protected void BtnOK_Click(object sender, EventArgs e)
        {
            if (IndexChange != null)
            {
                IndexChange(this, e);
            }
        }


    }
}